home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example / PhilosopherArea.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.9 KB  |  133 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. class PhilosopherArea extends Canvas {
  20.     PhilAnimator controller;
  21.  
  22.     static final double MARGIN = 10.0f;
  23.     static final int NUMPHILS = 5;
  24.  
  25.     Image[] imgs = new Image[3];
  26.  
  27.     double spacing;
  28.  
  29.     Philosopher[] philosophers = new Philosopher[NUMPHILS];
  30.     Chopstick[] chopsticks = new Chopstick[NUMPHILS];
  31.     boolean[] redraw = new boolean[NUMPHILS];
  32.     String[] names = { "Arisduktle" , "Dukrates", "Pythagorduke", "Duko", "Dukimedes" };
  33.  
  34.     boolean running = false;
  35.  
  36.     public PhilosopherArea(PhilAnimator controller) {
  37.         super();
  38.         this.controller = controller;
  39.  
  40.         MediaTracker mt;
  41.         mt = new MediaTracker(this);
  42.  
  43.         imgs[0] = controller.getImage(controller.getCodeBase(), "hungryduke.gif");
  44.         mt.addImage(imgs[0], 0);
  45.         imgs[1] = controller.getImage(controller.getCodeBase(), "rightspoonduke.gif");
  46.         mt.addImage(imgs[1], 1);
  47.         imgs[2] = controller.getImage(controller.getCodeBase(), "bothspoonsduke.gif");
  48.         mt.addImage(imgs[2], 2);
  49.  
  50.         try {
  51.             mt.waitForID(0);
  52.             mt.waitForID(1);
  53.             mt.waitForID(2);
  54.         } catch (java.lang.InterruptedException e) {
  55.             System.out.println("Couldn't load one of the images");
  56.         }
  57.  
  58.         spacing = imgs[0].getWidth(this) + MARGIN;
  59.         createPhilosophersAndChopsticks();
  60.     }
  61.  
  62.     public void paint(Graphics g) {
  63.         g.setColor(Color.lightGray);
  64.         g.fillRect(0, 0, size().width, size().height);
  65.         for (int i = 0; i < NUMPHILS; i++) {
  66.             redraw[i] = true;
  67.         }
  68.         update(g);
  69.     }
  70.  
  71.     public void update(Graphics g) {
  72.         for (int i = 0; i < NUMPHILS; i++) {
  73.             if (redraw[i]) {
  74.                 philosophers[i].paint(g);
  75.                 redraw[i] = false;
  76.             }
  77.         }
  78.     }
  79.  
  80.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  81.         if (running) {
  82.             for (int i = 0; i < NUMPHILS; i++)
  83.                 philosophers[i].suspend();
  84.         } else {
  85.             for (int i = 0; i < NUMPHILS; i++)
  86.                 philosophers[i].resume();
  87.         }
  88.  
  89.         running = !running;
  90.         return true;
  91.     }
  92.  
  93.     public synchronized void repaintPhil(int id) {
  94.         redraw[id] = true;
  95.         repaint();
  96.     }
  97.  
  98.     public void startPhilosophers() {
  99.         for (int i = 0; i < NUMPHILS; i++)
  100.             philosophers[i].start();
  101.         running = true;
  102.     }
  103.  
  104.     public void stopPhilosophers() {
  105.         for (int i = 0; i < NUMPHILS; i++)
  106.             philosophers[i].stopRequested();
  107.     }
  108.  
  109.     public void createPhilosophersAndChopsticks() {
  110.         double x, y;
  111.         double radius = 80.0;
  112.         double centerAdj = 85.0;
  113.         double radians;
  114.  
  115. /* for a straight line
  116.         y = MARGIN;
  117. */
  118.         for (int i = 0; i < NUMPHILS; i++)
  119.             chopsticks[i] = new Chopstick();
  120.  
  121.         for (int i = 0; i < NUMPHILS; i++) {
  122. /* for a straight line
  123.             x = i * spacing;
  124. */
  125.             radians = i*(2.0 * Math.PI /(double)NUMPHILS);
  126.             x = Math.sin(radians) * radius + centerAdj; 
  127.             y = Math.cos(radians) * radius + centerAdj; 
  128.             philosophers[i] = new Philosopher(this, x, y, i);
  129.             repaintPhil(i);   
  130.         }
  131.     }
  132. }
  133.